package cn.topevery.garden.wapi.utils;

import cn.topevery.garden.entity.po.Park;
import org.apache.commons.lang3.StringUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 The type Park empty util.
 *
 * @author wenzhao.lin
 * @created 2021 -07-27 18:49:48
 */
public class ParkEmptyUtil {

    /**
     * 描述 获取列表.
     *
     * @param list      the list
     * @param titleList the title list
     * @return the list
     * @throws Exception the exception
     * @author wenzhao.lin
     * @created 2021 -07-27 18:49:48 Get data list list.
     */
    public static List<Park> getDataList(List<List<String>> list, List<List<String>> titleList)throws Exception {
        List<Park> parkList = new ArrayList<>();
        List<String> titleCodeList = getCodeList(titleList);

        for(List<String> tempList:list){
            parkList.add(getEmpty(tempList, titleCodeList));
        }
        return parkList;
    }


    /**
     * 描述 获取标题列表.
     *
     * @param titleList the title list
     * @return the list
     * @throws Exception the exception
     * @author wenzhao.lin
     * @created 2021 -07-29 14:09:45 Get code list list.
     */
    private static List<String> getCodeList(List<List<String>> titleList)throws Exception {
        List<String> list = new ArrayList<>();
        for(List<String> tempList : titleList){
            for(int i=0; i<tempList.size(); i++){
                if(StringUtils.isNotEmpty(tempList.get(i)) && i > 0){
                    list.add(tempList.get(i));
                }
            }
        }
        return list;
    }


    /**
     * 描述 获取实体类.
     *
     * @param list     the list
     * @param codeList the code list
     * @return the park
     * @throws Exception the exception
     * @author wenzhao.lin
     * @created 2021 -07-27 18:49:48 Get empty park.
     */
    private static Park getEmpty(List<String> list, List<String> codeList)throws Exception {
        if(list.size() != codeList.size()){
            return null;
        }

        Map<String, Object> map = new HashMap<>();
        for (int i=0; i<list.size(); i++){
            map.put(codeList.get(i), list.get(i));
        }

        //Park park = JSON.parseObject(JSON.toJSONString(map), Park.class);
        Park park = (Park)convertMap(Park.class, map);
        return park;
    }


    /**
     * 描述 将Map转为对象.
     *
     * @param type the type
     * @param map  the map
     * @return the object
     * @throws Exception the exception
     * @author wenzhao.lin
     * @created 2021 -07-29 14:34:09 Convert map object.
     */
    private static Object convertMap(Class type, Map map) throws Exception {
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        Object obj = type.newInstance();
        //Field[] field = obj.getClass().getDeclaredFields();

        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor descriptor : propertyDescriptors) {
            String propertyName = descriptor.getName();

            if (map.containsKey(propertyName)) {
                Object value = map.get(propertyName);
                descriptor.getWriteMethod().invoke(obj, value);
            }
        }
        return obj;

    }
}
